package com.jd.rpa.script.jdl.commonutils;

import com.jd.rpa.sdk.domain.basic.Locator;
import com.jd.rpa.sdk.domain.component.AttachFile;
import com.jd.rpa.sdk.domain.component.SendMailParam;
import com.jd.rpa.sdk.domain.enums.AgreementType;
import com.jd.rpa.sdk.domain.enums.ExchangeEnum;
import com.jd.rpa.sdk.driver.RobotClientDriver;
import com.jd.rpa.sdk.script.RobotInvokeScriptAbstractService;
import org.ebot.script.FindFailed;
import org.ebot.script.Pattern;
import org.ebot.script.Screen;

import java.util.Collections;
import java.util.List;
import java.util.Map;


public class test extends RobotInvokeScriptAbstractService {
    private static final String ROBOT_EMAIL = "org.logistics.robo1";
    private static final String ROBOT_PASSWORD = "2023Rpa4.0";
    private static final String Email = "liudengzhong@jd.com";

    @Override
    public Object run(Map<String, Object> map) throws Exception {
        RobotClientDriver driver = driverManage.getWebDriverByProcessId(processId, null);
        try {
            Screen screen = new Screen();
            driver.get("http://erp.jd.com/");
            driver.windowMaximize();
            Thread.sleep(1000 * 5);
            driver.findElementByLocator(new Locator(Locator.Type.xpath,"//span[@class=\"qrcode-icon qrcode-icon-zh\"]")).click();
            Thread.sleep(1000 * 5);
            AttachFile attachFile = new AttachFile(screen.capture().getFile(), "test.png");
            sendMail(ROBOT_EMAIL, ROBOT_PASSWORD, "test", "test", Collections.singletonList(Email), Collections.singletonList(attachFile), null);
            Thread.sleep(1000 * 60);
          
            //driver.findElementByLocator(new Locator(Locator.Type.xpath,"/html/body/div[3]/div[1]/nav/div[1]/div[2]/button")).click();
            driver.findElementByLocator(new Locator(Locator.Type.xpath,"//div[@class=\"kuma-2545254248\"]")).click();
          
            Thread.sleep(500);
            //AttachFile attachFile2 = new AttachFile(screen.capture().getFile(), "test2.png");
            //sendMail(ROBOT_EMAIL, ROBOT_PASSWORD, "test2", "test2", Collections.singletonList(Email), Collections.singletonList(attachFile2), null);

          
        } catch (Exception e) {
            
        } finally {
            driver.close();
            driverManage.quitAll();
        }
        return null;
    }

    private void sendMail(String erp, String erpPwd, String subject, String content, List<String> mailList, List<AttachFile> attachFiles, List<String> Cc) {
        SendMailParam sendMailParam = new SendMailParam();
        //协议
        sendMailParam.setAgreement(AgreementType.EXCHANGE);
        //版本
        sendMailParam.setExchangeEnum(ExchangeEnum.Exchange2010);
        //服务地址
        sendMailParam.setMailServer("https://joymailews.jd.com/ews/Exchange.asmx");
        //域
        sendMailParam.setDomain("360buyad");
        //发件邮箱
        sendMailParam.setFrom(erp);
        //密码
        sendMailParam.setAuthCode(erpPwd);
        //收件人
        sendMailParam.setTo(mailList);
        //主题
        sendMailParam.setSubject(subject);
        //主题
        sendMailParam.setCc(Cc);
        //内容
        sendMailParam.setContent(content);
        //附件
        if (attachFiles != null) {
            sendMailParam.setAttachFiles(attachFiles);
        }
        mailUtils.sendMail(sendMailParam);
    }
    private void clickImage(Screen screen, String localPath) throws FindFailed, InterruptedException {
        if (screen.exists(new Pattern(localPath)) != null) {
            screen.click(new Pattern(localPath));
            Thread.sleep(3000);
        }
    }
}